package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.CalendarCategory;
import org.hgvc.webapp.ets.spring.service.PartnerService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.FacesUtils;


public class AdminPartnerProductCalendarMasterBean extends AbstractBean{

	public static final String BEAN_NAME="adminPartnerProductCalendarMasterBean";
	private PartnerService service;
	private CalendarCategory cat;
	private List<CalendarCategory> catList;
	
	public static final String NAV_EDIT_ACTION="adminPartnerProductCalCatEdit";
	public static final String NAV_CREATE_ACTION="adminPartnerProductCalCatCreate";
	public static final String NAV_LIST_ACTION="adminPartnerProductCalCatList";
	public CalendarCategory getCat() {
		return cat;
	}
	public void setCat(CalendarCategory cat) {
		this.cat = cat;
	}
	public void setService(PartnerService service) {
		this.service = service;
	}
	
	/*---------------------------------------------------------------*/
	public List<CalendarCategory> getCatList() {
		try{
			catList = service.getCalendarCategoryList();
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return catList;
	}
	
	public String navCreateAction(){
		cat = new CalendarCategory();
		return NAV_CREATE_ACTION;
	}
	
	public String createAction(){
		try{
			service.createCalendarCategory(cat, getThisUser());
			return NAV_LIST_ACTION;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		
		return NAV_CREATE_ACTION;
	}

	public void editActionListener(ActionEvent ae){
		UIParameter param = (UIParameter)ae.getComponent().findComponent("catId");
		String id = (String)param.getValue();

		for(CalendarCategory c:catList){
			if(c.getCalendarCatId().equalsIgnoreCase(id)){
				cat = c;
				break;
			}
		}
	}

	public String navEditAction(){
		return NAV_EDIT_ACTION;
	}

	public String editAction(){
		try{
			service.updateCalendarCategory(cat, getThisUser());
			return NAV_LIST_ACTION;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_EDIT_ACTION;
	}

	public String deleteAction(){
		try{
			service.deleteCalendarCategory(catList, getThisUser());
			return NAV_LIST_ACTION;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_LIST_ACTION;
	}
	
	public String navListAction(){
		return NAV_LIST_ACTION;
	}
	
	public void setupActionListener(ActionEvent ae){
		UIParameter param = (UIParameter)ae.getComponent().findComponent("vcatId");
		String id = (String)param.getValue();

		for(CalendarCategory c:catList){
			if(c.getCalendarCatId().equalsIgnoreCase(id)){
				cat = c;
				break;
			}
		}
	}
	public String setupAction(){
		AdminCalenderDtlMasterBean bean = (AdminCalenderDtlMasterBean)FacesUtils.getManagedBean(AdminCalenderDtlMasterBean.BEAN_NAME);
		return bean.init(cat);
	}
	
}
